﻿using Autofac.Integration.Mvc;
using Autofac;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using OA.DAL;
using System.Data.Entity;
using OA.IBLL;
using OA.Mvc.Filters;

namespace OA.Mvc.App_Start
{
	public class AutofacConfig
	{
		public static void Register()
		{
            var builder = new ContainerBuilder();
            //加载当前程序集
            Assembly controllerAss = Assembly.GetExecutingAssembly();
            //注册所有的Controller
            builder.RegisterControllers(controllerAss).PropertiesAutowired();
            // 注册全局过滤器
            builder.RegisterType<OAAuthorizeAttribute>().PropertiesAutowired().SingleInstance();
            builder.RegisterFilterProvider();



            //注册数据层程序集
            Assembly dalAss = Assembly.Load("OA.DAL");
            builder.RegisterAssemblyTypes(dalAss)
            //builder.RegisterTypes(dalAss.GetTypes())
                .PropertiesAutowired()
                .InstancePerLifetimeScope()
                .AsImplementedInterfaces();
            //注册业务层程序集
            Assembly bllAss = Assembly.Load("OA.BLL");
            builder.RegisterAssemblyTypes(bllAss)
                // builder.RegisterTypes(bllAss.GetTypes())
                .PropertiesAutowired()
                .AsImplementedInterfaces();
            // 单独注册“数据库上下文”，会覆盖前面的注册
            builder.RegisterType<OADbContext>().InstancePerLifetimeScope();

            var container = builder.Build();
            //当mvc创建Controller对象的时候，都是由Autofac为我们创建Controller对象
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

            var obj = container.Resolve<OAAuthorizeAttribute>();

        }
    }
}